package com.ucar.notice.notice.handler;

import com.ucar.base.intrdriver.notice.common.DriverNoticeConstants.NoticeStatusEnum;
import com.ucar.base.intrdriver.notice.common.DriverNoticeConstants.NoticeTypeEnum;
import com.ucar.base.vo.OpResult;
import com.ucar.common.OpResultCommon;
import com.ucar.notice.notice.handler.callback.InvokeMessageCallback;
import com.ucar.notice.notice.pojo.NoticeBean;
import com.ucar.notice.notice.service.NoticeHisService;
import com.ucar.notice.notice.service.NoticeService;
import com.ucar.util.CollectionsTools;
import com.ucar.util.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 具体下发司机通知处理类<br/>
 * @version V1.0  2017/5/30 21:42  by 石冬冬(dongdong.shi@ucarinc.com) - Heil Hitler
 */
@Service
public class NoticeIssueProcessor extends AbstractNoticeIssueProcessor {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeHisService noticeHisService;

    private List<NoticeBean>  notRequiredList;//不需要发送的通知list
    private List<NoticeBean> noDriverList;//没有添加司机的通知list

    @Override
    protected void filterDatas() {
        List<NoticeBean> noticeList = this.getNoticeList();
        if(CollectionsTools.isNotEmpty(noticeList)){
            this.noDriverList = new ArrayList<NoticeBean>(noticeList.size());
            this.notRequiredList = new ArrayList<NoticeBean>(noticeList.size());
            for (int i = 0; i < noticeList.size(); i++) {
                NoticeBean noticeBean = noticeList.get(i);
                //如果通知状态不是新建，则不需要发送
                NoticeStatusEnum noticeStatusEnum =  NoticeStatusEnum.getByIndex(noticeBean.getStatus());
                NoticeTypeEnum noticeTypeEnum = NoticeTypeEnum.getByIndex(noticeBean.getNoticeType());
                if(NoticeStatusEnum.NEW != noticeStatusEnum){
                    notRequiredList.add(noticeBean);
                    noticeList.remove(i);
                    i--;
                    continue;
                }
                //如果通知类型为部分司机，并且通知下没有配置司机，则不需要发送
                if(NoticeTypeEnum.PART_NOTICE == noticeTypeEnum && noticeBean.getNoticeConfigNum() < 1){
                    noDriverList.add(noticeBean);
                    noticeList.remove(i);
                    i--;
                    continue;
                }
            }
        }
    }

    @Override
    protected OpResult<String> checkDatas(InvokeMessageCallback messageCallback) {
        OpResult<String> result = new OpResult<String>(true,"下发成功");
        if(messageCallback.needCheck()){
            String message = messageCallback.call(this);
            if(StringTools.isNotEmpty(message)){
                OpResultCommon.setFailureResult(result,message);
            }
            if(messageCallback.allowIssueWhenUnvalid()){
                result.setSuccess(true);
            }
        }
        return result;
    }

    @Override
    protected void after() {
        if(!this.hasNoticeList()){
            logger.info("【司机通知下发】没有下发通知");
            return;
        }
        List<Long> noticeIdList = CollectionsTools.getList(this.getNoticeList(), new CollectionsTools.FieldGetter<Long, NoticeBean>() {
            @Override
            public Long getFiled(NoticeBean o) {
                return o.getId();
            }
        });
        this.noticeService.setStatusToSend(noticeIdList);
        //添加历史记录
        this.noticeHisService.saveHistory(noticeIdList);
    }

    public List<NoticeBean> getNotRequiredList() {
        return notRequiredList;
    }

    public List<NoticeBean> getNoDriverList() {
        return noDriverList;
    }

    public boolean hasNoticeList(){
        return null != this.getNoticeList() && this.getNoticeList().size() > 0;
    }
}
